# -*- coding:utf-8 -*-

from basic_1 import web_static
from lxml import etree
from xlsxwriter import Workbook
import os, shutil, time


class amazon_pic():

    def __init__(self, path):
        self.path = path
        self.home_url = "https://www.amazon.com"
        self.next_url = []

    def get_all_elements(self, url):
        web = web_static(url)
        web_etree = etree.HTML(web)
        product_lst = []
        product_info = web_etree.xpath('//div[@class="s-item-container"]/div[3]/div[1]/a/@*')
        product_pics_url = web_etree.xpath('//div[@class="s-item-container"]/div[2]/div[1]//img/@src')
        self.next_url = web_etree.xpath('//div[@class="pagnHy"]/span[@class="pagnRA"]/a/@href')

        for i in range(len(product_info)):
            if i % 3 == 1:
                file_name = product_info[i].strip()
                file_link = product_info[i + 1].strip()
                product_lst.append([file_name, file_link])
        return product_lst, product_pics_url

    def saveImg(self, product_pic_url):
        pics_path = []
        len_lst = len(product_pic_url)
        for pic_url in product_pic_url:
            len_lst -=1
            print len_lst
            name = self.path + '/img/' + pic_url.split('/')[-1]
            pics_path.append(name)
            print pic_url
            try:
                img = web_static(pic_url.strip())
                time.sleep(1)
                with open(name, 'wb') as p:
                    p.write(img)

            except:
                print "download PIC failure: %s" % pic_url
                img = self.path + r"/downloadfailure.jpg"
                with open(name, 'wb') as p:
                    p.write(img)
                continue

        return pics_path

    def create_dir(self, dname):
        try:
            if u'\u2013' in dname:
                dname = str(dname.replace(u'\u2013', ''))
            if u'"' in dname:
                dname = str(dname.replace(u'"', ''))
            dir_name = self.path + dname
            if os.path.exists(u"%s" % dir_name) is False:
                os.makedirs(u"%s" % dir_name)
            return dir_name
        except:
            print "create dir failure"
            print dname

    def write_data_excel(self, data1, data2):
        name = 'amazon'
        create_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))
        file_name = self.path+'/%s_%s.xlsx' % (name, create_time)
        new_f = Workbook(file_name)
        sheet1 = new_f.add_worksheet(create_time)
        sheet1_format = new_f.add_format({})
        sheet1_format.set_text_wrap()
        sheet1.set_column(0,0, 25)
        sheet1.set_column(1,4, 60)
        print len(data1), len(data2)
        for i in range(len(data1)):
            if i != 0:
                sheet1.set_row(i,125)
                sheet1.insert_image(i, 0, data2[i])
            for j in range(len(data1[i])):
                if i == 0:
                    sheet1.set_row(0, 18)
                    headers = ['PRODUCE NAME', 'AMAZON LINK']
                    sheet1.write(0,j+1,headers[j])
                else:
                    sheet1.write(i, (j+1), data1[i][j],sheet1_format)
        new_f.close()

    def gogogogo(self, short_url):
        product_lst = []
        pics_lst = []

        for i in range(100):
            latest_url = short_url
            if len(self.next_url) == 0:
                url = self.home_url + latest_url
            else:
                url = self.home_url + self.next_url[0]
                latest_url = self.next_url[0]
            print url
            tmp_product_lst, tmp_pics_lst = self.get_all_elements(url)
            product_lst.extend(tmp_product_lst)
            pics_lst.extend(tmp_pics_lst)
            print len(product_lst),len(pics_lst)
            time.sleep(2)
        latest_url = self.path + "/page_urls.txt"
        with open(latest_url,'a+') as f_url:
            f_url.write(self.next_url[0]+'\n')
        pics_all_path = self.saveImg(pics_lst)
        self.write_data_excel(product_lst, pics_all_path)


if __name__ == "__main__":

    #sorted by featured
    url2 = "/s/ref=lp_3733481_st?rh=n%3A1055398%2Cn%3A%211063498%2Cn%3A1063306%2Cn%3A1063318%2Cn%3A3733481&qid=1504349274&sort=featured-rank"
    path = "/root/log"
    img_path = "/root/log/img"
    if os.path.exists(img_path):
        shutil.rmtree(img_path)
    os.makedirs(path+"/img")

    pic = amazon_pic(path)
    pic.gogogogo(url2)

